/**
 * Created by xuelei.kong on 2016/5/24.
 */
$(function () {
        var table= $('#dataTable').dataTable({
            "bAutoWidth":false,
            "bInfo": false,
            "bFilter": false,
            "bPaginate": false,
            "bScrollCollapse": true,
            "aoColumns": [
                {"bSortable": false},
                {"bSortable": false},
                null, null, null, null, null, null
            ]
        });
    function setPeopleListHeight() {
        $('.table-responsive').height(document.documentElement.clientHeight * 0.7);
    }

    function winResize(fn) {
        var timer;
        clearTimeout(timer);
        $(window).resize(function () {
            timer = setTimeout(fn, 300);
        });
    }

    setPeopleListHeight();
    winResize(setPeopleListHeight);
    $('table th input:checkbox').on('click', function () {
        var that = this;
        $(this).closest('table').find('tr > td:first-child input:checkbox')
            .each(function () {
                this.checked = that.checked;
                $(this).closest('tr').toggleClass('selected');
            });

    });

    $('#dataTableWrap tbody .ace').on("click", function () {
        var flag = true;
        $(this).closest('tr').toggleClass('selected');
        $('#dataTableWrap tbody .ace').each(function (i) {
            if (!this.checked) {
                flag = false;
                return false;
            }
        })
        $('.check_all .ace')[0].checked = flag;
    })

    $("#delete_bash").on("click", function () {
        if ($('#dataTableWrap tbody .ace:checked').length) {
            bootbox.confirm({
                buttons: {
                    confirm: {
                        label: '确定'
                    },
                    cancel: {
                        label: '取消'
                    }
                },
                message: "确定删除?",
                callback: function (result) {
                    if (result) {
                        $('#dataTableWrap tbody .ace:checked').each(function () {
                            $(this).parents('tr').remove();
                        })
                    }
                }
            });
        }

    });
    /*单个删除*/
    $('.delete-btn').on("click", function () {
        var tr = $(this).parents('tr');
        bootbox.confirm({
            buttons: {
                confirm: {
                    label: '确定'
                },
                cancel: {
                    label: '取消'
                }
            },
            message: "确定删除?",
            callback: function (result) {
                if (result) {
                    table.fnDeleteRow(tr,function (settings,data) {
                        console.log(data);
                    })
                }
            }
        });
    })
    /*查看*/
    $('.view-btn').on("click", function () {
        $.get('pictures_detail.html',{"userID":1},function (data) {
            bootbox.dialog({
                message: data,
                buttons
                    :
                {
                    "success"
                        :
                    {
                        "label"
                            :
                            "<i class='icon-ok'></i> Close",
                        "className"
                            :
                            "btn-sm btn-success",
                        "callback"
                            :
                            function () {
                                //Example.show("great success");
                            }
                    }
                }
            })
        },"html");

        ;
    })

    var totalPage=10;
    function pagination(total,curr,callback){
        var page_btn='<div class="pagination">';
        if(curr==1){
            page_btn+='<a class="pg-prev pg-prev-disabled"><i class="icon-double-angle-left"></i></a>';
        }else{
            page_btn+='<a data-page="'+(curr-1)+'" href="javascript:void(0)" class="pg-prev"><i class="icon-double-angle-left"></i></a>';
        }
        if(curr-1>3){
            page_btn+='<a data-page="1" href="javascript:void(0)" class="pg-page pg-item">1</a>';
            page_btn+='<span class="pg-item">…</span>';
            for(var j=curr-2;j<curr;j++){
                page_btn+='<a data-page="'+j+'" href="javascript:void(0)" class="pg-page pg-item">'+j+'</a>';
            }
        }else{
            for(var i=1;i<curr;i++){
                page_btn+='<a data-page="'+i+'" href="javascript:void(0)" class="pg-page pg-item">'+i+'</a>';
            }

        }
        page_btn+='<span class="pg-current pg-item">'+curr+'</span>';
        if(curr+3>total){
            for(var k=curr;k<total;k++){
                page_btn+='<a data-page="'+(k+1)+'" href="javascript:void(0)" class="pg-page pg-item">'+(k+1)+'</a>';
            }
        }else{
            page_btn+='<a data-page="'+(curr+1)+'" href="javascript:void(0)" class="pg-page pg-item">'+(curr+1)+'</a>';
            page_btn+='<a data-page="'+(curr+2)+'" href="javascript:void(0)" class="pg-page pg-item">'+(curr+2)+'</a>';
            if(curr+3<total){
                page_btn+='<span class="pg-item">…</span>';
            }
            page_btn+='<a data-page="'+total+'" href="javascript:void(0)" class="pg-page pg-item">'+total+'</a>';

        }

        if(curr==total){
            page_btn+='<a class="pg-next pg-next-disabled"><i class="icon-double-angle-right"></i></a>';
        }else{
            page_btn+='<a data-page="'+(curr+1)+'" href="javascript:void(0)" class="pg-next"><i class="icon-double-angle-right"></i></a>';
        }
        page_btn+="</div>";
        typeof callback=='function' && callback();
        $('#pagination').empty().append(page_btn);

    }
    pagination(totalPage,1);

})
